package org.example.view.console;

import org.dom4j.DocumentException;
import org.example.entity.*;
import org.example.service.AdminService;
import org.example.service.CinemaService;
import org.example.service.IAdminService;
import org.example.service.ICinemaService;
import org.example.util.TicketFactory;

import java.io.IOException;
import java.text.ParseException;
import java.util.Scanner;

/**
 * @author TonySong
 * @date 2025/7/29 0029
 * @time 11:01
 */
public class CinemaView {
    private ICinemaService cinemaService =new CinemaService();
    private AdminService adminService =new AdminService();
    private static final Scanner in = new Scanner(System.in);
    public void menu(Admin admin) throws IOException, DocumentException, ParseException, ClassNotFoundException {
        int m = 0;
        String name="";
        cinemaService.readXmlToMap();
        if(ICinemaService.SEAL_TICKETS_FILE.exists()){
            System.out.println("是否要读取历史记录（y/n）");
            String ans=in.next();
            if(ans.equalsIgnoreCase("y")){
                cinemaService.loadTickets();
            }
        }
        do {
            System.out.println("1: 浏览电影 2：购买   3：查询销售记录  4：继续购票  5：读取历史记录  6：退出");
            m = in.nextInt();
            switch (m) {
                case 3:
                    cinemaService.showSealTickets();
                    break;
                case 6:
                    System.out.println("退出");
                    adminService.saveAdmin(admin);
                    //判断是否存在销售记录
                    if(ICinemaService.SEAL_TICKETS.size()>0){
                        System.out.println("当前存在销售记录，是否要进行保存（y/n）");
                        String ans=in.next();
                        if(ans.equalsIgnoreCase("y")){
                            cinemaService.saveTickets();
                        }
                    }
                    System.exit(0);
                    break;
                case 1:
                    //读取电影信息
                    cinemaService.showMovies();
                    //判断是否存在销售记录

                    break;
                    case 2:
                        System.out.println("请输入电影名称：");
                        name=in.next();
                        Movie movie = cinemaService.getMovieByName(name);
                        if(movie!=null){
                            System.out.println(movie);
                            System.out.println("您选择的是："+movie.getName()+"确认码？(y/n)");
                            String ans=in.next();
                            if(ans.equalsIgnoreCase("y")){
                                System.out.println("请选择：1：普通 2：学生 3：赠票");
                                 m=in.nextInt();
                                 //利用工厂模式产生电影票
                                Ticket ticket = TicketFactory.createTicket(movie, m);
                                if(ticket instanceof StudentTicket){
                                    System.out.println("请输入折扣：");
                                    int discount=in.nextInt();
                                    ((StudentTicket) ticket).setDiscount(discount);
                                }
                                if(ticket instanceof FreeTicket){
                                    System.out.println("请输入客户姓名：");
                                    name=in.next();
                                    ((FreeTicket) ticket).setCustomerName(name);
                                }
                                //打印电影票
                                ticket.print();
                                //保存到销售集合中
                                cinemaService.addTickets(ticket);
                            }
                        }else{
                            System.out.println("没有该电影");
                        }
                        break;
            }
        } while (m <= 6);
    }
}
